// components/carpopup/carpopup.js
import Toast from '@vant/weapp/toast/toast';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    popShow:{
      type:Boolean,
      value:false
    },
    selectObj:{
      type:Object,
      value:{
      }
    },
    stepShow:{
      type:Boolean,
      value:false
    },
    carShow:{
      type:Boolean,
      value:true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    mySelect:{},
  },

  /**
   * 组件的方法列表
   */
  methods: {
//直接购买
    clickBuy(){
      var vail = this.validata();
      if(!vail)
     {
       return ;
     }
    
    if(!this.data.mySelect.num)
    {
      this.data.mySelect.num=1
    }
    var mySelect = JSON.stringify([this.data.mySelect])
      wx.navigateTo({
       url: '/pages/orders/orders?goodArr='+mySelect,
     })
    },

    clickCart(){
      //需要点击一下进行初始化
     var vail = this.validata();
     wx.showLoading({
      title:'数据加载中',
      mask:true
    })
     if(!vail)
     {
       return ;
     }
     else{
        wx.cloud.callFunction({
        name:"xiangcun_cart_add",
        data:{
          mySelect:this.data.mySelect
        }
      }).then(res=>{
        wx.showToast({
          title: '添加成功',
          mask:true
        })
            this.onClose();
            wx.hideLoading()
      })
     }
     
    },

    validata(){

      this.setData({
        "mySelect.goodid":this.data.selectObj._id,
        "mySelect.title":this.data.selectObj.title,
        "mySelect.picurl":[this.data.selectObj.picurl[0]],
        "mySelect.price":this.data.selectObj.price,
      })
      wx.hideLoading()
      return true
    },

    //关闭弹窗
    onClose(){
      this.setData({
        "mySelect.goodid":this.data.selectObj._id,
        "mySelect.title":this.data.selectObj.title,
        "mySelect.picurl":[this.data.selectObj.picurl[0]],
        "mySelect.price":this.data.selectObj.price,
      })
      this.triggerEvent("popSelect",{mySelect:this.data.mySelect})
      this.setData({
        popShow:false,
        mySelect:{}
      })
    },

    clickNum(e){
      this.setData({
        "mySelect.num":e.detail
      })
    },

    clickSize(e){
      this.setData({
        "mySelect.size":e.currentTarget.dataset.size,
        "mySelect.num":1
      })
    },
    clickColor(e){
      this.setData({
        "mySelect.color":e.currentTarget.dataset.color,
        "mySelect.num":1
      })
    }
  }
})
